<?php
/**
 * Created by IntelliJ IDEA.
 * User: admin
 * Date: 5/28 0028
 * Time: 14:04
 */

namespace app\index\model;


class Tool
{

    /**
     * 导入Excel
     * @param $filename
     * @param $encode
     * @param $file_type
     * @return array
     */
    public static function importExcel($filename, $encode, $file_type) {
        if (strtolower($file_type) != 'xls' && strtolower($file_type) != 'xlsx') {
            die("不支持该文件类型");
        }
        if (strtolower($file_type) == 'xls') {//判断excel表类型为2003还是2007
            $objReader = \PHPExcel_IOFactory::createReader('Excel5');
        } else{
            $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
        }
        $objReader->setReadDataOnly(true);
        $objPHPExcel = $objReader->load($filename);
        $objWorksheet = $objPHPExcel->getActiveSheet();
        $highestRow = $objWorksheet->getHighestRow();
        $highestColumn = $objWorksheet->getHighestColumn();
        $highestColumnIndex = \PHPExcel_Cell::columnIndexFromString($highestColumn);
        $excelData = [];
        for ($row = 1; $row <= $highestRow; $row++) {
            for ($col = 0; $col < $highestColumnIndex; $col++) {
                $excelData[$row][] = (string) $objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
            }
        }
        return $excelData;
    }

}